#!/usr/bin/env python
# -*- coding: utf-8 -*-
# <bitbar.title>Pi-hole status</bitbar.title>
# <bitbar.version>v3.1</bitbar.version>
# <bitbar.author>Felipe Martin</bitbar.author>
# <bitbar.author.github>fmartingr</bitbar.author.github>
# <bitbar.author>Siim Ots</bitbar.author>
# <bitbar.author.github>siimots</bitbar.author.github>
# <bitbar.desc>Show summary and manage Pi-Hole from menubar.</bitbar.desc>
# <bitbar.image>https://files.fmartingrlabs.com/github/bitbar-plugins/pihole.1m.png</bitbar.image>
# <bitbar.dependencies>pi-hole,python</bitbar.dependencies>
import json
import os

try:  # Python 3
    from urllib.request import urlopen
except ImportError:  # Python 2
    from urllib2 import urlopen  # noqa


PLUGIN_PATH = os.path.join(os.getcwd(), __file__)

# ---
# Variables
# ---

# URL to the pi-hole admin path without trailing slash
base_url = "http://pi.hole/admin"

# Your Pi-hole password hash (used for management)
# THIS IS NOT YOUR PIHOLE ADMIN PASSWORD
# You can find this password hash in the setupVars.conf file of your pihole
# server which is typically is found in /etc/pihole/setupVars.conf.
# Look for the PASSWORDHASH key.
password = ""

# Menubar icon type ('color' or 'bw')
icon_type = 'bw'

# Menubar icon
icon_bw = 'iVBORw0KGgoAAAANSUhEUgAAACQAAAAkCAYAAADhAJiYAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAWJQAAFiUBSVIk8AAAActpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IlhNUCBDb3JlIDUuNC4wIj4KICAgPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4KICAgICAgPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIKICAgICAgICAgICAgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIgogICAgICAgICAgICB4bWxuczp0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AAAAAAAAP///wD///8A////AP///wD///8AAP///wD///8A////AP///wD///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAqAACB5QAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA/+wAAP+jAAD/MgAAcQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA////AP///wD///8A////AP///wAA////AP///wD///8A////AP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALAAAf90AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD/4AAA/7gAAP/cAAD/JgAAbwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AAD///8A////AP///wD///8A////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADQAAHjdAAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP/oAAD/kgAA/+wAAP/bAAD/JAAAawAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP///wD///8A////AP///wD///8AAP///wD///8A////AP///wD///8AAAAAAAAAAAAAAAAAAAAAAAAAAAA2AAB14QAA//8AAP/6AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA/90AAP+UAAD/9gAA//8AAP/TAAD/HwAAXgAAAAADAAAAAQAAAAAAAAAAAAAA////AP///wD///8A////AP///wAA////AP///wD///8A////AP///wAAAAAAAAAAAAAAAAAAAAAADgAAbp8AAP/HAAD/mQAA/5cAAP++AAD/+QAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD/lgAA/5UAAP//AAD//wAA//8AAP/LAAD+IgAAVSAAAAAUAAAAAAAAAAAAAAD///8A////AP///wD///8A////AAD///8A////AP///wD///8A////AAAAAAAAAAAAAAAAAAgAAGyiAAD/6AAA/9UAAP/ZAAD/xQAA/3sAAP9PAAD/wwAA//8AAP//AAD//wAA//8AAP//AAD//wAA/8YAAP8/AAD/2wAA//8AAP//AAD//wAA//8AAP+9AAD6HwAATwkAAAAAAAAAAAAAAP///wD///8A////AP///wD///8AAP///wD///8A////AP///wD///8AAAAAAAAAAAAYAABcxQAA//8AAP//AAD//wAA//8AAP//AAD//wAA/44AAP8KAAD/dAAA/9sAAP//AAD//wAA//8AAP/AAAD/FQAA/5QAAP//AAD//wAA//8AAP//AAD//wAA//8AAP+oAADxBwAAPAAAAAAAAAAA////AP///wD///8A////AP///wAA////AP///wD///8A////AP///wAAAAAAAAAAG54AAOL/AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA/4YAAP8AAAD/EAAA/0IAAP9hAAD/QwAA/wAAAP9eAAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP94AADKAAAAAAAAAAD///8A////AP///wD///8A////AAD///8A////AP///wD///8A////AAAAACwuAACI7AAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD/5QAA/yAAAP8AAAD/AAAA/wAAAP8AAAD/BQAA/9cAAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA/9sAAP0NAABVAAAAC////wD///8A////AP///wD///8AAP///wD///8A////AP///wD///8AGQAAiG0AAOX/AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP/9AAD/UAAA/wAAAP8AAAD/AAAA/wAAAP9IAAD/+QAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD/+gAA/0oAAM0DAABu////AP///wD///8A////AP///wACAAAAAAAAAAAAAAAAAAAAAAAAAAAJAAALCAAABwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPwAAAD4AAAAAAAAAAAAAAAAAAAAAAAAAB0AAAAGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFAAAAEwAAIQcAACcAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAOAAALXPAAC++AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA5wAAAM4AAAAAAAAAAAAAAAAAAAAAAAAA8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPUAAADiAADI9gAAwwAAAAAAAAAAAAAAAAAAAAAAAAAAAP///wD///8A////AP///wD///8AAAAAAAIAADvSAADz/wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP+YAAD/AAAA/wgAAP8VAAD/CAAA/wAAAP8MAAD/2wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD/0gAA8wEAADsAAAAA////AP///wD///8A////AP///wAA////AP///wD///8A////AP///wAAAAAAAAAAAEkAAKH8AAD//wAA//8AAP//AAD//wAA//8AAP//AAD/1gAA/w8AAP9ZAAD/vQAA/9oAAP++AAD/gAAA/xAAAP87AAD/9AAA//8AAP//AAD//wAA//8AAP//AAD//wAA//wAAP9IAACeAAAAAAAAAAD///8A////AP///wD///8A////AAD///8A////AP///wD///8A////AAAAAAAAAAAAAAAAEnMAAL//AAD//wAA//8AAP//AAD//wAA//8AAP9JAAD/eAAA//8AAP//AAD//wAA//8AAP//AAD/5QAA/2AAAP9eAAD/1AAA//8AAP//AAD//wAA//8AAP/sAAD/VQAAugAAAA4AAAAAAAAAAP///wD///8A////AP///wD///8AAP///wD///8A////AP///wD///8AAAAAAAAAAAAAAAAAAAAAE3UAAMb/AAD//wAA//8AAP//AAD/oAAA/1kAAP/2AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA/78AAP+IAAD/mgAA/64AAP/GAAD/zAAA/zgAAMIAAAAPAAAAAAAAAAAAAAAA////AP///wD///8A////AP///wAA////AP///wD///8A////AP///wAAAAAAAAAAAAYAAAANAAAAAAAAF3cAAM//AAD//wAA//8AAP9/AAD/xwAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//gAAP/SAAD/xAAA//AAAP9kAADLAAAAFQAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AAD///8A////AP///wD///8A////AAAAAAAAAAAABAAAABUAAAAVAAAAAAAAHHIAAMr/AAD/4AAA/44AAP/rAAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD/gAAAygkAABsJAAAAAAAAAAAAAAAAAAAAAAAAAP///wD///8A////AP///wD///8AAP///wD///8A////AP///wD///8AAAAAAAAAAAAEAAAAFAAAABYAAAATAAAAAAAAHIAAAMLzAAD/mAAA/+oAAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA/3cAAMYAAAAeHgAAAAkAAAAAAAAAAAAAAAAAAAAAAAAA////AP///wD///8A////AP///wAA////AP///wD///8A////AP///wAAAAAAAAAAAAQAAAAUAAAAFgAAABUAAAAMAAAAAQAAFHMAAMfFAAD/9QAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP9+AADNAAAAGREAAAAgAAAACQAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AAD///8A////AP///wD///8A////AAAAAAAAAAAABAAAABQAAAAWAAAAFAAAABQAAAANAAAADQAAF0QAAM/oAAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD/hwAA2wMAAB0SAAAAGgAAACAAAAAJAAAAAAAAAAAAAAAAAAAAAAAAAP///wD///8A////AP///wD///8AAP///wD///8A////AP///wD///8AAAAAAAAAAAAEAAAAFAAAABYAAAAUAAAAEgAAABUAAAAiAAAAAAAAHV4AAMv/AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA/4cAANsMAAAnHgAAABkAAAAYAAAAIAAAAAkAAAAAAAAAAAAAAAAAAAAAAAAA////AP///wD///8A////AP///wAA////AP///wD///8A////AP///wAAAAAAAAAAAAQAAAAUAAAAFgAAABQAAAASAAAAFQAAACQAAAAKAAAAAAAAHGsAALz2AAD//wAA//8AAP//AAD//wAA//IAAP9tAAC/DAAAJyQAAAAfAAAAGQAAABgAAAAgAAAACQAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AAH///8AAAAAAAAAAAAAAAAAAAAAAAEBAQAAAAAABAAAABAAAAACAAAA/gAAAP4AAAADAAAADwAAAOcAAAD+AAAA9wAABioAAGtgAABeOQAAHPsAAP/BAADepQAAoNwAAJ4jAAD6AwAAAPkAAAD6AAAA/wAAAAgAAADpAAAA9wAAAAAAAAAAAAAAAAAAAP///wAAAAAAAAAAAAAAAAAAAAAAAQAA//8wabirP4J+hgAAAABJRU5ErkJggg==' # noqa

# Url to check the service status
url_status = "%s/api.php?status&auth=%s" % (base_url, password)

# Url to get the summary
url_summary = "%s/api.php?summary" % base_url

# Urls to enable/disable service
url_enable = "%s/api.php?enable&auth=%s" % (base_url, password)
url_disable = "%s/api.php?disable&auth=%s" % (base_url, password)


# ---
# Helper methods
# ---
def convert_to_native(data):
    return json.loads(data)


def do_request(url, method='GET'):
    response = urlopen(url)
    return convert_to_native(response.read())


def get_summary():
    response = do_request(url_summary)
    return response


def get_status():
    response = do_request(url_status)
    return response['status']


def separator():
    print('---')


# Data
summary = get_summary()
status = get_status()
enabled = status == 'enabled'


# Layout
def bitbar():
    # Menubar icon
    print('| templateImage=%s' % globals()['icon_%s' % icon_type])
    separator()

    print("Open pi-hole admin | href=%s" % base_url)
    separator()

    print('Status: %s' % status)
    if enabled:
        print('Disable Pi-hole | href=%s' % url_disable)
    else:
        print('Enable Pi-hole | href=%s' % url_enable)

    separator()
    print("Domains being locked: %s" % summary['domains_being_blocked'])
    print("Ads blocked today: %s (%s%%)" % (summary['ads_blocked_today'],
                                            summary['ads_percentage_today']))
    print("DNS queries today: %s" % summary['dns_queries_today'])
    print("Queries cached today: %s" % summary['queries_cached'])
    print("Queries forwarded today: %s" % summary['queries_forwarded'])
    print("Unique domains today: %s" % summary['unique_domains'])
    separator()


# Execution
try:
    bitbar()
except Exception as e:
    print('Script error:')
    print(e)
    separator()
